﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using Entities;
namespace Stationery
{
    public partial class UC_Order : System.Web.UI.UserControl
    {
        BLL_City bll_City = new BLL_City();
        BLL_Order bll_Order = new BLL_Order();
        BLL_Customer bll_Customer = new BLL_Customer();
        BLL_Product bll_Product = new BLL_Product();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session[Utils.SESSION_ORDER] = null;
                Set_DataSource_Grid();
            }
            else
            {
                if (Session[Utils.SESSION_ORDER] != null)
                {
                    ASPxGridView1.DataSource = Session[Utils.SESSION_ORDER];
                    ASPxGridView1.DataBind();
                }
            }
            //if (Page.IsCallback)
            //{
            //    Set_DataSource_Grid();
            //}
        }
        private void Set_DataSource_Grid()
        {
            BLL_MenuURL bll_MenuURL = new BLL_MenuURL();
            DevExpress.Web.ASPxGridView.GridViewDataComboBoxColumn gridColumn = ASPxGridView1.Columns["comboboxDetail"] as DevExpress.Web.ASPxGridView.GridViewDataComboBoxColumn;
            DevExpress.Web.ASPxGridView.GridViewDataTextColumn gridColumnPrice = ASPxGridView1.Columns["colprice"] as DevExpress.Web.ASPxGridView.GridViewDataTextColumn;
            gridColumn.PropertiesComboBox.DataSource = bll_MenuURL.Get_MenuURL_All();
            //gridColumn.PropertiesComboBox.TextField = "SpelledOutNumbers";
            //gridColumn.PropertiesComboBox.ValueField = "ActualNumbers";
            Session[Utils.SESSION_ORDER] = bll_Order.Get_AllProduct_Order();
            if (Session[Utils.SESSION_CART] != null)
            {
                List<Order> lstBasket = (List<Order>)Session[Utils.SESSION_CART];
                List<Order> lstOrder = (List<Order>)Session[Utils.SESSION_ORDER];
                foreach (Order basket in lstBasket)
                {
                    foreach (Order order in lstOrder)
                    {
                        if (basket.ID_PRODUCT == order.ID_PRODUCT)
                        {
                            order.QUANTITY = basket.QUANTITY;
                            UpdatePriceValue(order);
                            break;
                        }
                    }
                }
                Session[Utils.SESSION_ORDER] = lstOrder;
            }
            ASPxGridView1.DataSource = Session[Utils.SESSION_ORDER];
            ASPxGridView1.DataBind();
        }

        protected void ASPxButtonUpdate_Click(object sender, EventArgs e)
        {
            if (Session[Utils.SESSION_ORDER] != null)
            {
                //chỉ lọc ra những sản phẩm có số lượng đặt > 0 sau đó đổ vào giỏ
                List<Order> lstOrder = new List<Order>();
                List<Order> lstBasket = new List<Order>();
                lstOrder = (List<Order>)Session[Utils.SESSION_ORDER];
                foreach (Order order in lstOrder)
                {
                    if (order.QUANTITY > 0)
                    {
                        lstBasket.Add(order);
                    }
                }
                Session[Utils.SESSION_CART] = lstBasket;
                Response.Redirect(UtilsURL.order);
            }
        }

        protected void ASPxButtonContinue_Click(object sender, EventArgs e)
        {
            Response.Redirect(UtilsURL.home);
        }

        private void UpdatePriceValue(Order pOrder)
        {
            pOrder.VALUE = pOrder.PRICE * pOrder.QUANTITY;
            pOrder.TOTAL = pOrder.VALUE + pOrder.VALUE * pOrder.TAX;
        }

        protected void ASPxGridView1_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
        {
            string strParam = e.Parameters.ToString();
            string[] lstString =  strParam.Split(',');
            int intID_PRODUCT = Convert.ToInt32(lstString[0]);
            int intNUMBER = Convert.ToInt32(lstString[1]);
            if (Session[Utils.SESSION_ORDER] != null)
            {
                List<Order> lstOrder = new List<Order>();
                lstOrder = (List<Order>)Session[Utils.SESSION_ORDER];
                foreach (Order order in lstOrder)
                {
                    if (order.ID_PRODUCT == intID_PRODUCT)
                    {
                        order.QUANTITY = intNUMBER;
                        UpdatePriceValue(order);
                        break;
                    }
                }
                Session[Utils.SESSION_ORDER] = lstOrder;
                ASPxGridView1.DataSource = Session[Utils.SESSION_ORDER];
                ASPxGridView1.DataBind();
            }
        }
    }
}
